package com.santiao.youxiang.ui.fragment

import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.santiao.youxiang.AppConst
import com.santiao.youxiang.R
import com.santiao.youxiang.dialog.InputPasswordDialogPopup
import com.santiao.youxiang.event.XXLEvent
import com.santiao.youxiang.ui.activity.RYWebViewActivity
import com.santiao.youxiang.ui.activity.RYContactCustomerServiceActivity
import com.santiao.youxiang.utils.DeviceUtils
//import com.santiao.youxiang.utils.GetHttpDataUtil
import com.santiao.youxiang.utils.UserInfoModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SetFragment : Fragment() {

    val TAG = "SetFragment"

    private lateinit var user_iv_photo: ImageView
    private lateinit var privacyLinearLayout: LinearLayout
    private lateinit var protocolLayout: LinearLayout
    private lateinit var user_fk: LinearLayout
    private lateinit var my_tv_version: TextView
    private lateinit var my_tv_dj_number: TextView
    private lateinit var my_container: FrameLayout
    var stat = 0;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return  inflater.inflate(R.layout.fragment_set,container,false)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if(!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initView(view)
    }

    private fun initView(view: View){
        privacyLinearLayout=view.findViewById(R.id.user_privacy)
        protocolLayout=view.findViewById(R.id.user_protocol)
        user_fk=view.findViewById(R.id.user_fk)
        my_tv_version=view.findViewById(R.id.my_tv_version)
        my_tv_dj_number=view.findViewById(R.id.my_tv_dj_number)
        user_iv_photo=view.findViewById(R.id.user_iv_photo)
        my_container= view.findViewById(R.id.my_container)

        my_tv_version.setText(DeviceUtils.getVersionName(requireActivity()))
        my_tv_dj_number.setText(UserInfoModel.getDjid())
        privacyLinearLayout.setOnClickListener({
            RYWebViewActivity.forward(
                requireActivity(),
                AppConst.URL_PRIVACY_POLICY,
                getString(R.string.privacy_policy)
            )
        })


        protocolLayout.setOnClickListener({
            RYWebViewActivity.forward(
                requireActivity(),
                AppConst.URL_USER_AGREEMENT,
                getString(R.string.user_agreement)
            )
        })

        user_iv_photo.setOnClickListener({
            stat++;
            Log.i("countDownTimerstat", "${stat}")
            if (stat == 1) {
                countDownTimer.start()
            }
            if (stat > 5) {
                stat = 0
                showInputPasswordDialog()
            }
        })

        user_fk.setOnClickListener({
            RYContactCustomerServiceActivity.Companion.show(requireActivity());
        })
    }


    override fun onResume() {
        Log.i("testing","setFragment on resume")


        super.onResume()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageSimple(t: XXLEvent) {

    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    var InputPopupView: BasePopupView? = null
    private fun showInputPasswordDialog() {
        if (InputPopupView?.isShow == true) {
            return
        }
        val customPopup =
            InputPasswordDialogPopup(requireActivity())
        customPopup.listener = object : InputPasswordDialogPopup.OnInputPasswordListener {
            override fun cancel() {
            }

            override fun ok(password: String) {
                if (null != password && !TextUtils.isEmpty(password.trim())) {
                    InputPopupView?.dismiss()
//                    GetHttpDataUtil.setWhiteListHttp(password.trim())
                } else {
                    ToastUtils.showLong("密码为空")
                }
            }


        }
        InputPopupView = XPopup.Builder(requireActivity())
            .autoOpenSoftInput(false)
            .autoDismiss(false)
//            .enableDrag(false)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asCustom(customPopup)
            .show()
    }


    /**
     * CountDownTimer 实现倒计时
     */
    private val countDownTimer: CountDownTimer = object : CountDownTimer(3000, 1000) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            stat = 0
            Log.i("countDownTimerstat", "${stat}")
        }
    }
}